﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Kooboo.Commerce;
using System.Web.Mvc;
using Kooboo.Connect;
using Kooboo.Commerce.Services;
using Kooboo.Web.Mvc;

namespace Kooboo.Web.Controllers
{
    public class ControllerBase : Controller
    {
        IVistor _Actor;
        protected IVistor Actor
        {
            get
            {
                if (this._Actor == null)
                {

                    this._Actor = new Vistor();
                }

                return this._Actor;
            }
        }

        #region BindViewData

        #region BindAll
        protected virtual void BindViewData()
        {
            this.BindStatusBar();
            this.BindSidebar();
            this.BindSearchBar();
            this.BindLinks();
            this.BindCartBox();

        }
        #endregion

        #region StatusBar
        void BindStatusBar()
        {
            

            if (this.Request.IsAuthenticated)
            {
                this.ViewBag.StatusBar = "StatusBar_Logged";
                var user = Kooboo.IoC.ContextContainer.Current.Resolve<User>();

                var fullName = user.Profile.FirstName + " " + user.Profile.LastName;

                this.ViewBag.LoggedName = string.IsNullOrWhiteSpace(fullName) ? user.Name : fullName;

                this.ViewBag.StatusLinks = new
                {
                    LogOn = this.Url.Action<StatusController>(m => m.LogOn()),
                    Register = this.Url.Action<RegisterController>(m => m.Index()),
                    LogOff = this.Url.Action<StatusController>(m => m.LogOff()),
                    Profile = this.Url.Action<AccountController>(m=>m.Index())
                };
            }
            else
            {
                this.ViewBag.StatusBar = "StatusBar_Guest";

                this.ViewBag.StatusLinks = new
                {
                    LogOn = this.Url.Action<StatusController>(m => m.LogOn()),
                    Register = this.Url.Action<RegisterController>(m => m.Index()),
                    LogOff = this.Url.Action<StatusController>(m => m.LogOff()),
                    Profile = "#",
                };
            }
        }
        #endregion

        #region Sidebar
        void BindSidebar()
        {
            var service = new BrandService();


            var Brands = service.GetAllBrands()
               .Select(i => new
               {
                   Name = i.Name,
                   Id = i.Id
               })
               .ToArray()
               .Select(i => new
               {
                   i.Name,
                   Url = this.Url.Action<BrandController>(m => m.Index(i.Id, null, null)),
               });
            this.ViewBag.Brands = Brands;
        }

        protected virtual void Select(IBrand brand)
        {
            ViewData["Brand"] = brand;


            var items = new Stack<IBrand>();

            
            if (brand != null)
            {
                items.Push(brand);
               
            }

            var last = items.Count - 1;

            this.ViewBag.Breadcrumb = items.Select((i, index) => new
            {
                Name = i.Name,
                Url = this.Url.Action<BrandController>(n => n.Index(i.Id, null, null)),
                Separator = index == last ? "" : ">"
            });
        }

        protected virtual void Select(ICategory category)
        {
            ViewData["Category"] = category;

            var items = new Stack<ICategory>();

            var next = category;
            while (next != null)
            {
                items.Push(next);
                next = next.Parent;
            }

            var last = items.Count - 1;

            this.ViewBag.Breadcrumb = items.Select((i,index) => new
            {
                Name = i.Name,
                Url = this.Url.Action<CategoryController>(n => n.Index(i.Id, null, null)),
                Separator = index == last ? "" : ">"
            });
        }

        #endregion

        #region SearchBar
        void BindSearchBar()
        {
            this.ViewBag.SearchAction = this.Url.Action<SearchController>(m => m.Index(null, null, null, null));

            var items = new List<SelectListItem>();

            var service = new CategoryService();
            foreach (var i in service.GetAllCategories().Where(m => m.Parent == null))
            {
                Enumerate(i, "·", items);
            }

            this.ViewBag.SearchItems = items;

        }

        void Enumerate(ICategory category, string indent, List<SelectListItem> items)
        {
            items.Add(new SelectListItem { Text = indent + category.Name, Value = category.Id.ToString() });
            foreach (var i in category.Children)
            {
                Enumerate(i, indent + indent, items);
            }
        }
        #endregion

        #region Links
        void BindLinks()
        {
            this.ViewBag.Links = new
            {
                About = this.Url.Action<ContentController>(m=>m.About()),
                Contact = this.Url.Action<ContentController>(m=>m.Contact()),
                FAQ = this.Url.Action<ContentController>(m=>m.Help()),
                Privacy = this.Url.Action<ContentController>(m=>m.Privacy()),
                Terms = this.Url.Action<ContentController>(m=>m.Terms())
            };
        }
        #endregion

        #region BindCart
        void BindCartBox()
        {
            this.ViewBag.CartBox = new
            {
                Url = this.Url.Action<CartController>(m=>m.Index()),
                Count = this.OpenCart().Items.Count
            };
        }
        #endregion
        #endregion

        
        protected virtual Cart OpenCart()
        {
            var cart = this.ViewData["Cart"] as Cart;

            if (cart == null)
            {
                var service = new CartService();

                this.ViewData["Cart"] = cart = service.OpenCart();
            }

            return cart;
        }

      
    }
}